Skip to content

Fix unstable formatting with comments on optional parens#5241

Merged
cobaltt7 merged 3 commits into
psf:mainfrom
l46983284-cpu:fix-3701-unstable-multi-pass-comment
Jul 21, 2026
Merged

Fix unstable formatting with comments on optional parens#5241
cobaltt7 merged 3 commits into
psf:mainfrom
l46983284-cpu:fix-3701-unstable-multi-pass-comment

Conversation

@l46983284-cpu

@l46983284-cpu l46983284-cpu commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes multi-pass instability when an inline comment sits on optional parentheses (assert message / assignment RHS). Omitting those parens re-parented the comment onto a different leaf, so the RHS splitter chose a different shape on the next pass and tripped the second-pass stability check.

can_omit_invisible_parens now returns false when the optional opening paren already carries an inline comment, which pins the comment and keeps the formatting stable. Covers the #3701 reproducer and the same class from #3706 / #4384.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the
    stability policy?
    (N/A — this is an unstable-formatting bug fix, not a deliberate style change.)
  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?
    (N/A — unintended formatting bug; no style-guide rewrite.)

Fixes #3701

Don't omit optional parentheses when the opening paren already carries
an inline comment. Omitting them re-parents the comment after the next
parse and can change the RHS split on a second pass (psf#3701, psf#3706,
psf#4384).

Signed-off-by: Alex Chen <l46983284@gmail.com>
@l46983284-cpu
l46983284-cpu force-pushed the fix-3701-unstable-multi-pass-comment branch from 5ffc355 to 3535e22 Compare July 17, 2026 11:41
@github-actions

github-actions Bot commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (4426338) to main (51abf53):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

Comment thread CHANGES.md Outdated
@cobaltt7
cobaltt7 merged commit 42ed836 into psf:main Jul 21, 2026
57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unstable formatting: Comment is moved to lower line twice

2 participants